package utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import ar.edu.utn.frsf.cidisi.upcolbpip2pn.generator.petrinet.Upcolbpip2PN;

import utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocol;

/**
 * @generated
 */
public class UpcolbpipVerifyInteractionProtocolAction implements IObjectActionDelegate {

	private static String TARGETDIR = "";

	/**
	 * @generated
	 */
	private IWorkbenchPart targetPart;

	/**
	 * @generated
	 */
	private URI domainModelURI;

	/**
	 * @generated
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.targetPart = targetPart;
	}

	/**
	 * @generated
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		domainModelURI = null;
		action.setEnabled(false);
		if (selection instanceof IStructuredSelection == false
				|| selection.isEmpty()) {
			return;
		}
		IFile file = (IFile) ((IStructuredSelection) selection)
				.getFirstElement();
		domainModelURI = URI.createPlatformResourceURI(file.getFullPath()
				.toString(), true);
		action.setEnabled(true);
	}

	/**
	 * @generated
	 */
	private Shell getShell() {
		return targetPart.getSite().getShell();
	}

	/**
	 * @generated
	 */
	public void run(IAction action) {
		TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE
				.createEditingDomain();
		ResourceSet resourceSet = editingDomain.getResourceSet();
		EObject diagramRoot = null;
		try {
			Resource resource = resourceSet.getResource(domainModelURI, true);
			diagramRoot = (EObject) resource.getContents().get(0);
		} catch (WrappedException ex) {
			UpcolbpipDiagramEditorPlugin.getInstance().logError(
					"Unable to load resource: " + domainModelURI, ex); //$NON-NLS-1$
		}
		if (diagramRoot == null) {
			MessageDialog
					.openError(
							getShell(),
							Messages.UpcolbpipInitDiagramFileAction_InitDiagramFileResourceErrorDialogTitle,
							Messages.UpcolbpipInitDiagramFileAction_InitDiagramFileResourceErrorDialogMessage);
			return;
		}
		
		if (diagramRoot instanceof Diagram){
			InteractionProtocol ip = (InteractionProtocol)((Diagram)diagramRoot).getElement();
			
			createPages(ip);
			
			MessageDialog
			.openInformation(
					getShell(),
					"UP-ColBPIP",
					"Petri Net specification was generated on " + ip.getName());
		}
		
	}
	
	private void createPages(InteractionProtocol ip) {
		Upcolbpip2PN upcolbpip2PN = new Upcolbpip2PN();
		FileWriter output;
		BufferedWriter writer;

		try {
			output = new FileWriter(TARGETDIR + ResourcesPlugin.getWorkspace().getRoot().getLocation()+"/VMI Collaboration/Verification/" + ip.getName() + ".cpn");
			writer = new BufferedWriter(output);
			writer.write(upcolbpip2PN.generate(ip));
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
			
	}
	
}
